import { ref } from 'vue'

const useCheckbox = () => {
    const list = ref(new Set())

    const toggle = (item) => {
        if (list.value.has(item)) {
            list.value.delete(item)
        } else {
            list.value.add(item)
        }
    }

    const toggleAll = (items) => {
        if (list.value.size === items.length) {
            list.value.clear()
        } else {
            items.forEach((_, index) => list.value.add(index))
        }
    }

    const isChecked = (item) => {
        return list.value.has(item)
    }

    const reset = () => {
        list.value.clear()
    }

    return {
        list,
        toggle,
        toggleAll,
        isChecked,
        reset
    }
}

export { useCheckbox }
